package cn.itsource.product.service.impl;

import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessageQuery;
import cn.itsource.pet.domain.StateIdsDto;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDto;
import cn.itsource.product.domain.TProductDetail;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.mapper.TProductDetailMapper;
import cn.itsource.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    TProductDetailMapper tProductDetailMapper;

    @Override
    public PageList pagelist(SearchMasterMessageQuery query) {
        return productMapper.pagelist(query);
    }

    @Override
    public AjaxResult save(ProductDto productDto) {
        productDto.getProduct().setState(0l);
        productDto.getProduct().setCreatetime(new Date());
        productMapper.insertSelective(productDto.getProduct());
        productDto.getProductDetail().setProductId(productDto.getProduct().getId());
        tProductDetailMapper.insertSelective(productDto.getProductDetail());
        return AjaxResult.me();
    }

    @Override
    public AjaxResult upbutton(StateIdsDto stateIdsDto) {
        Product product = new Product();
        if(Objects.equals(stateIdsDto.getState(), 1)){
            product.setOnsaletime(new Date());
        }else {
            product.setOffsaletime(new Date());
        }
        return productMapper.upbutton(stateIdsDto,product);
    }

    @Override
    public AjaxResult deleteBatch(List<Long> ids) {
        productMapper.deleteBatch(ids);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult delete(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
        return AjaxResult.me();
    }

    @Override
    public TProductDetail findById(Long id) {
        return tProductDetailMapper.findById(id);
    }

    @Transactional
    @Override
    public AjaxResult EEsave(ProductDto productDto) {
        productMapper.updateByPrimaryKeySelective(productDto.getProduct());
        tProductDetailMapper.updateByPrimaryKeySelective(productDto.getProductDetail());
        return AjaxResult.me();
    }

    @Override
    public PageList findAll(SearchMasterMessageQuery query) {
        return productMapper.findAll(query);
    }

    @Override
    public Product proFindOne(Long id) {
        return productMapper.proFindOne(id);
    }
}
